import logging
import allure
import pytest
import requests
from help_api.api_keyword.api_key import ApiKey
from help_api.params.allParams import URL

# 逻辑层
class sarech_order():
    token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJvcGVuaWQiOiJvNkttbTVFQnhZY0JvRmVmTFVNMTRMSUdJZU93IiwidHlwZSI6Ind4bSIsImlkIjoxMzQ1LCJyb2xlX2lkIjozLCJpYXQiOjE3NDQ2MzI3NTIsImV4cCI6MTc0NDY3NTk1Mn0.MVm6e8M8fnRtAGaAlwrfIuu3A3CBpGhlU9NjKo_V-KM'
    """登录"""
    def test_login(self):
        allure.dynamic.title("登录")
        apiKey = ApiKey()
        userInfo = {
            "js_code": "0e3AaY100QWUWT1X7u000XkK1k4AaY17"
        }
        url = f"{URL}/api/wx/user/login"
        response = apiKey.post(
            url=url,
            json=userInfo,
            headers={"Content-Type": "application/json"}
        )
        with allure.step(f"发送登录请求"):
            msg = apiKey.get_text(response.text, 'msg')
            print(f"响应消息：{msg}")
            assert msg == '登录成功'


    """获取广告营销列表"""
    def test_List(self):
        apikey = ApiKey()
        # apiKey, token, res, token_random, uid = token_fix  # 从fix中获取预置的工具类和token和uid
        allure.dynamic.title('获取广告营销列表')  # 动态获取参数生成标题
        with allure.step(f"获取广告营销列表"):
            # 准备请求数据
            json = {
                "uid": "14172",
            }
            url = f"{URL}/api/calousels/get"  # 构建完整的 URL
            headers = {'token':self.token}
            response = apikey.post(url=url, headers=headers, json=json)
        with allure.step(f"接口返回信息校验接打印"):
            result = response.json()  # 获取响应结果（只调用一次 json()）
            msg = result.get('msg', '未找到msg字段')  # 获取响应消息
            # print(f"响应消息：{msg}")
            # # 断言响应消息
            # assert result.get('msg') == '获取成功', (
            #     f"响应消息与预期不符\n"
            #     f"预期消息：{'获取成功'}\n"
            #     f"实际消息：{result.get('msg')}"
            # )
            #数据库断言
            # assert apiKey.db_mysql_select(table='dl_server',field='id',value=apiKey.get_text(response.text,'data[0].id'))!='','未找到对应服务'

    """点击搜索"""
    def test_serach(self):
        apikey=ApiKey()
        allure.dynamic.title('搜索')  # 动态获取参数生成标题
        # apiKey, token, res, token_random, uid = token_fix  # 从fix中获取预置的工具类和token和uid
        with allure.step('发送搜索请求'):
            res1=self.test_List()
            url = f"{URL}/api/calousels/get"  # 构建完整的 URL
            json = {
                "state": "4",
                "id": apikey.get_text(res1.text,'oid')
            }
            headers = {'token':self.token}
            response = apikey.post(url=url, headers=headers, json=json)
        with allure.step(f"接口返回信息校验接打印"):
            result = response.json()  # 获取响应结果（只调用一次 json()）
            msg = result.get('msg', '未找到msg字段')  # 获取响应消息
            print(f"响应消息：{msg}")
            # # 断言响应消息
            # assert result.get('msg') == '操作成功', (
            #     f"响应消息与预期不符\n"
            #     f"预期消息：{'操作成功'}\n"
            #     f"实际消息：{result.get('msg')}"
            # )
        return response
